//
//  XXTZCell.m
//  FruitsWholesaler
//
//  Created by sgone-2 on 16/4/28.
//  Copyright © 2016年 sgone. All rights reserved.
//

#import "XXTZCell.h"
#import "Common.h"
#import "labelAdaptive.h"
#import "UIView+Helper.h"

@interface XXTZCell  () {
    
    

    
}


/**
 发信人
 */
@property (nonatomic, strong)UILabel *nameLabel;
/**
 发送时间
 */
@property (nonatomic, strong)UILabel *timeLabel;
/**
 发送内容
 */
@property (nonatomic, strong)UILabel *contentLabel;
/**
 发信人
 */
@property (nonatomic, strong)UILabel *sendLabel;
/**
 询价内容
 */
@property (nonatomic, strong)UILabel *titleLabel;
/**
 回复
 */
@property (nonatomic, strong)UILabel *replyLabel;
/**
 背景view
 */
@property (nonatomic, strong)UIView *backView;


@end


@implementation XXTZCell

- (void)awakeFromNib {
    [super awakeFromNib];
    
    self.backgroundColor = [UIColor clearColor];
    self.contentView.backgroundColor = [UIColor clearColor];

}



- (UILabel *)nameLabel {
    
    if (_nameLabel == nil) {
        
        _nameLabel = [[UILabel alloc] init];
        _nameLabel.font = [UIFont systemFontOfSize:14 / FDLaOt];
        _nameLabel.textColor = FDFont_COLOR;
        [self.backView addSubview:_nameLabel];
    }
    return _nameLabel;
}
 
/*
- (UILabel *)sendLabel {
    
    if (_sendLabel == nil) {
        
        _sendLabel = [[UILabel alloc] init];
        _sendLabel.font = [UIFont systemFontOfSize:14 / FDLaOt];
        _sendLabel.textColor = FDFont_COLOR;
        _sendLabel.textAlignment = NSTextAlignmentRight;
        [self.backView addSubview:_sendLabel];
    }
    return _sendLabel;
}
 */


- (UILabel *)timeLabel {
    
    if (_timeLabel == nil) {
        
        _timeLabel = [[UILabel alloc] init];
        _timeLabel.font = [UIFont systemFontOfSize:13 / FDLaOt];
        _timeLabel.textColor = FDFont_COLOR;
        _timeLabel.textAlignment = NSTextAlignmentRight;
        [self.backView addSubview:_timeLabel];
    }
    return _timeLabel;
}

- (UILabel *)contentLabel {
    
    if (_contentLabel == nil) {
        
        _contentLabel = [[UILabel alloc] init];
        _contentLabel.font = [UIFont systemFontOfSize:13 / FDLaOt];
        _contentLabel.textColor = FDMFont_COLOR;
        _contentLabel.numberOfLines = 3;
        [self.backView addSubview:_contentLabel];
    }
    return _contentLabel;
}

- (UILabel *)titleLabel {
    
    if (_titleLabel == nil) {
        
        _titleLabel = [[UILabel alloc] init];
        _titleLabel.font = [UIFont systemFontOfSize:13 / FDLaOt];
        _titleLabel.textColor = FDMFont_COLOR;
        _titleLabel.textAlignment = NSTextAlignmentRight;
        [self.backView addSubview:_titleLabel];
    }
    return _titleLabel;
}

- (UILabel *)replyLabel {
    
    if (_replyLabel == nil) {
        
        _replyLabel = [[UILabel alloc] init];
        _replyLabel.font = [UIFont systemFontOfSize:13 / FDLaOt];
        _replyLabel.textColor = FDMainColorAla;
        _replyLabel.numberOfLines = 0;
        [self.backView addSubview:_replyLabel];
    }
    return _replyLabel;
}

- (UIView *)backView {
    
    if (_backView == nil) {
        
        _backView = [[UIView alloc] init];
        _backView.backgroundColor = [UIColor whiteColor];
        _backView.layer.cornerRadius = 5;
        [self addSubview:_backView];
    }
    return _backView;
}

+ (instancetype)cellWithTableView:(UITableView *)tableVIew {
    
    NSString *ID = @"XXTZCell";
    XXTZCell *cell = [tableVIew dequeueReusableCellWithIdentifier:ID];
    if (cell == nil) {
        
        cell = [[[NSBundle mainBundle] loadNibNamed:@"XXTZCell" owner:nil options:nil] lastObject];
    }
    return cell;
}


- (CGFloat)getCellHeight {
    
    self.sendLabel.text = @"收信人：";
    self.nameLabel.text = self.messageDic[@"username"];
    self.titleLabel.text = @"询价内容：";
    
    //时间戳转时间
    double lastactivityInterval = [self.messageDic[@"inputtime"] doubleValue];
    NSDateFormatter* formatter = [[NSDateFormatter alloc] init];
    formatter.timeZone = [NSTimeZone timeZoneWithName:@"shanghai"];
    [formatter setDateStyle:NSDateFormatterMediumStyle];
    [formatter setTimeStyle:NSDateFormatterShortStyle];
    [formatter setDateFormat:@"yyyy-MM-dd"];
    NSDate *date = [NSDate dateWithTimeIntervalSince1970:lastactivityInterval];
    
    self.timeLabel.text = [NSString stringWithFormat:@"发送时间：%@",[formatter stringFromDate:date]];
    self.contentLabel.text = self.messageDic[@"chart_text"];
//    self.contentLabel.backgroundColor = [UIColor greenColor];
    NSString *replyStr = nil;
    
    switch ([self.messageDic[@"chart_status"] integerValue]) {
            
        case 0:
            
            replyStr = @"    未读";
            self.replyLabel.textColor = FDMainColorAla;
            break;
        case 1:
            
            replyStr = @"    未读";
            self.replyLabel.textColor = FDMainColorAla;
            break;
        case 2:
            
            replyStr = @"    已读";
            self.replyLabel.textColor = FDGFont_COLOR;
            break;
        case 3:
            
            replyStr = @"    已读";
            self.replyLabel.textColor = FDGFont_COLOR;
            break;
            
        default:
            break;
    }
    self.replyLabel.text = replyStr;
    
    CGFloat Y = 8;
    CGFloat Height = 20 / FDLaOt;
    
//    CGSize sendSize = [labelAdaptive sizeWithString:self.sendLabel.text font:self.sendLabel.font];
//    self.sendLabel.frame = CGRectMake(ViewWidth / 5 - sendSize.width, Y, sendSize.width, Height);
    
    CGSize nameSize = [labelAdaptive sizeWithString:self.nameLabel.text font:self.nameLabel.font];
    
    self.nameLabel.frame = CGRectMake(8, Y, nameSize.width, Height);
    
    CGSize timeSize = [labelAdaptive sizeWithString:self.timeLabel.text font:self.timeLabel.font];
    self.timeLabel.frame = CGRectMake(ViewWidth - 8 * 2 - timeSize.width - 8, self.nameLabel.y, timeSize.width, Height);
    
    CGSize titleSize = [labelAdaptive sizeWithString:self.titleLabel.text font:self.titleLabel.font];
    self.titleLabel.frame = CGRectMake(8, self.nameLabel.y + self.nameLabel.height + Y +8, titleSize.width, Height);
    
    CGSize contentSize = [labelAdaptive sizeWithString:self.contentLabel.text font:self.contentLabel.font width:ViewWidth - 8 * 2 - ViewWidth / 5 - 8];
    
    if (contentSize.height < 20 / FDLaOt) {
        
        self.contentLabel.frame = CGRectMake(ViewWidth / 5, self.titleLabel.y+3, ViewWidth - 8 * 2 - ViewWidth / 5 - 8, contentSize.height);
    } else if (contentSize.height > 15 / FDLaOt * 3) {
        
        self.contentLabel.frame = CGRectMake(ViewWidth / 5, self.titleLabel.y+3, ViewWidth - 8 * 2 - ViewWidth / 5 - 8, 17 / FDLaOt * 3);
    } else {
        
        self.contentLabel.frame = CGRectMake(ViewWidth / 5, self.titleLabel.y+3, ViewWidth - 8 * 2 - ViewWidth / 5 - 8, 17 / FDLaOt * 2);
    }
    
    CGSize replySize = [labelAdaptive sizeWithString:self.replyLabel.text font:self.replyLabel.font];
    self.replyLabel.frame = CGRectMake(ViewWidth - 8 * 2 - 8 - replySize.width, self.contentLabel.y + self.contentLabel.height, replySize.width, Height);
    
    self.backView.frame = CGRectMake(8, 0, ViewWidth - 8 * 2, self.replyLabel.y + self.replyLabel.height + 4);
    
    return self.backView.height;
}




- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

  
}

@end
